﻿using UnityEngine;
using System.Collections;

public class test : MonoBehaviour {
    public BoxCollider2D c;
	// Use this for initialization
	void Start () {
       
	}
	
	// Update is called once per frame
	void Update () {

      //  Debug.Log( GetWorldBounds(c) );
       // Debug.Log(bounds(c));
        Debug.Log("x:" + Min(c).x + "|" + Max(c).x + "y:" + Min(c).y + "|" + Max(c).x);

	}
   
    public  Vector2 Max(this Collider2D collider)
    {
        if (collider is BoxCollider2D)
            return (Vector2)collider.gameObject.transform.position +
            new Vector2(collider.gameObject.transform.localScale.x * (((BoxCollider2D)collider).center.x + (((BoxCollider2D)collider).size.x / 2)),
                        collider.gameObject.transform.localScale.y * (((BoxCollider2D)collider).center.y + (((BoxCollider2D)collider).size.y / 2)));
        Debug.LogError("Unable to determine max for this collider");
        return Vector2.zero;
    }

    public  Vector2 Min(this Collider2D collider)
    {
        if (collider is BoxCollider2D) return (Vector2)collider.gameObject.transform.position +
            new Vector2(collider.gameObject.transform.localScale.x * (((BoxCollider2D)collider).center.x - (((BoxCollider2D)collider).size.x / 2)),
                        collider.gameObject.transform.localScale.y * (((BoxCollider2D)collider).center.y - (((BoxCollider2D)collider).size.y / 2)));

        Debug.LogError("Unable to determine min for this collider");
        return Vector2.zero;
    }
    public Rect bounds(BoxCollider2D collider)
    {
        float maxX, maxY, minX, minY;
 
      
        minX = collider.transform.TransformPoint(collider.center -collider.size*0.5f).x;
        maxX = collider.transform.TransformPoint(collider.center + collider.size*0.5f).x;

         minY = collider.transform.TransformPoint(collider.center - collider.size * 0.5f).y;
        maxY = collider.transform.TransformPoint(collider.center + collider.size * 0.5f).y;

       
        return new Rect(minX,maxY,maxX,minY);
    }
    public  Rect GetWorldBounds( BoxCollider2D boxCollider2D)
    {
        float worldRight = boxCollider2D.transform.TransformPoint(boxCollider2D.center + (boxCollider2D.size * 0.5f)).x;
        float worldLeft = boxCollider2D.transform.TransformPoint(boxCollider2D.center - (boxCollider2D.size * 0.5f)).x;

        float worldTop = boxCollider2D.transform.TransformPoint(boxCollider2D.center +( boxCollider2D.size * 0.5f)).y;
        float worldBottom = boxCollider2D.transform.TransformPoint(boxCollider2D.center - ( boxCollider2D.size * 0.5f)).y;

        //float worldRight = boxCollider2D.transform.TransformPoint(boxCollider2D.center + new Vector2(boxCollider2D.size.x * 0.5f, 0)).x;
        //float worldLeft = boxCollider2D.transform.TransformPoint(boxCollider2D.center - new Vector2(boxCollider2D.size.x * 0.5f, 0)).x;

        //float worldTop = boxCollider2D.transform.TransformPoint(boxCollider2D.center + new Vector2(0, boxCollider2D.size.y * 0.5f)).y;
        //float worldBottom = boxCollider2D.transform.TransformPoint(boxCollider2D.center - new Vector2(0, boxCollider2D.size.y * 0.5f)).y;

    

       
        return new Rect(worldLeft,worldTop,worldRight- worldLeft,worldTop- worldBottom);
       
    
    }
}
